import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams,AlertController } from 'ionic-angular';
import { HTTP } from '@ionic-native/http';
import { ChongPage } from '../chong/chong';



// import { ChangeDetectorRef } from '@angular/core'; 


import { Events } from 'ionic-angular';


import { JiluPage } from '../jilu/jilu';

import {IndexPage} from "../index/index";//传的数据
/**
 * Generated class for the DhzqPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@Component({
  selector: 'page-dhzq',
  templateUrl: 'dhzq.html',

})
export class DhzqPage {
  huabi={
    "money":"0",
    "username":"请登录",
    // "image":"http://gp.huirn.cn/statics/uploads/shopimg/20160414/84687000634817.jpg",
    "name":"商品",
    // "imagebase":"http://gp.huirn.cn/statics/uploads/",

  } 
   
   public pageNo:number = 1;
   public data_list:Array<any>=[];
   public data_list2:Array<any>=[];
   
    public jia:Array<any>=[];
     public url:string ="http://118.31.79.20:8080/"; 
    //  public url:string ="http://192.168.1.114:8080/";
    // public imgurl:string="http://118.31.79.20/statics/uploads/";   //img地址
    public spurl:string=this.url+"Stock/product/findProductList.do?pageNo="+this.pageNo; //商品接口
    public duiurl:string = this.url + "Stock/pay/pay.do"    //兑换接口

    userid=localStorage.getItem("userid");
    username=localStorage.getItem("username");
    userpassword=localStorage.getItem("userpassword");


    constructor(public navCtrl: NavController, public navParams: NavParams,private http: HTTP,public events: Events,private malert:AlertController) {
     
      this.load();
      // this.onload();
  }


  // 充值
  chong(){
        if(this.userid==''||this.userid==null){
         this.mWind("您好，请先登录账户!!");
          this.navCtrl.push(IndexPage);
        }else{
             this.navCtrl.push(ChongPage);
        }
  }
  // 兑换记录
  duihuan(){
        if(this.userid==''||this.userid==null){
          this.mWind("您好，请先登录账户!!");
          this.navCtrl.push(IndexPage);
        }else{
           this.navCtrl.push(JiluPage);
        }
     
  }

  //兑换
  dui(t,m,i){
     if(this.userid==''||this.userid==null){
         this.mWind("您好，请先登录账户!!");
          this.navCtrl.push(IndexPage);
     }else{
        let nu = Number(m);
          let xian = Number(this.huabi.money);
          if( nu > xian){           
            this.mWind("您的牛牛币不足，不能直接兑换");
            this.navCtrl.push(ChongPage);
          }else {
            let alert= this.malert.create({
                      title :"提示",
                      message:"是否确认使用" + nu +"枚牛牛币兑换" + t,
                      buttons:[{
                        text:'取消',
                        handler:()=>{         
                        }
                      },{
                        text:'确定',
                        handler:()=>{ 
                              this.http.post(this.duiurl, {"uid":this.userid,"shopid":i}, {})
                          .then(data => {
                            //获取的数据
                            var data_shop=JSON.parse(data.data);
                            if(data_shop.flag == true){    //成功跳转等页面
                                //传参
                                this.mWind("兑换成功,请到兑换记录填写地址");
                                this.load();
                            }else{
                                this.mWind("兑换失败");
                                return false;
                            }
                            
                          })
                          .catch(error => {
                             this.mWinds("网络异常");
                          
                          });  
                        }
                      }],
                      
                    });
                  alert.present();
              
          }
     }
    
    
  }
  
  /**
   * 网络请求
   */
  ionViewWillEnter(){
    this.load();
    // console.log("兑换");
  }

  load(){
      let logisurl:string ="http://118.31.79.20:8080/Stock/login/login.do?username="+this.username+'&&password='+ this.userpassword; 
         // 个人信息请求
            this.http.get(logisurl, {}, {})
             .then(data => {
                //获取的数据
                var datass=JSON.parse(data.data);
               
                this.huabi.money = datass.data.money; //用户牛牛币
                //数据改变
                // alert(IndexPage.userid);
            })
            .catch(error => {                            
                  this.mWinds("网络失败");
                //  alert(error);
            });

      // this.huabi.money=IndexPage.usermoney; //牛牛币
      this.huabi.username=this.username;//账户名
      // 第一个请求
      this.http.get(this.spurl, {}, {})
      .then(data => {
        //获取的数据
        var data_data=JSON.parse(data.data);
        var  data_root=data_data.currentPageDatas;
        this.jia = data_data.currentPageDatas;
        for(var  k in data_root){
          this.data_list.push(data_root[k]);

        }
      })
      .catch(error => {
      
          // alert(error);
      
      });
  }


/*加载更多数据*/
   doInfinite(c) {
     this.pageNo++;
     this.spurl=this.url+"Stock/product/findProductList.do?pageNo="+this.pageNo;
     //alert("ss"+this.spurl);
         this.load();
    setTimeout(()=>{
      //完成加载
    c.complete();
    },3000);
   }

     /**
    * 自定义弹框
    */
    mWind(msg){
        let alert= this.malert.create({
      title :"提示",
      message:msg,
      buttons:[{
        text:'取消',
        handler:()=>{         
        }
      },{
        text:'确定',
        handler:()=>{       
        }
      }],
      
    });
  alert.present();

    }
    /**
     * 无按钮
     * @param msg 
     */
    mWinds(msg){
        let alert= this.malert.create({
      title :"提示",
      message:msg,     
    });
  alert.present();

    }
}
